package com.fjwt.gz.service.activity.other.acte026;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.cache.RedisUtil;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.db.bo.ActivityAwardAndSettingBo;
import com.fjwt.gz.db.bo.ActivityUserTimesBO;
import com.fjwt.gz.db.dto.FansActivityDTO;
import com.fjwt.gz.db.vo.ActivityAwardVO;
import com.fjwt.gz.service.activity.BaseActivityService;
import com.fjwt.gz.service.impl.ActivityAwardService;
import com.fjwt.gz.service.impl.ActivitySettingService;
import com.fjwt.gz.service.impl.AwardSettingService;
import com.fjwt.gz.service.impl.SendAwardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 商家券领取------
 */
@Slf4j
@Service
public class ACTE026Service extends BaseActivityService<Object> {

    private static String logPrefix = "【商家券领取】";
    @Autowired
    private ActivityAwardService activityAwardService;
    @Autowired
    private SendAwardService sendAwardService;
    @Autowired
    private AwardSettingService awardSettingService;
    @Autowired
    private ActivitySettingService activitySettingService;

    /**
     * 兑换奖品
     */
    @Override
    public ActivityAwardVO takePartIn(FansActivityDTO fansActivityDTO) {
        log.info("进入{}", logPrefix);
        super.takePartIn(fansActivityDTO);

        log.info("用户基本信息：{}", JSONUtil.toJsonStr(fansActivityDTO));
        List<ActivityAwardAndSettingBo> activityAwardList = awardSettingService.getActivityAwardList(fansActivityDTO.getActivityId(), redisOutTime);
        // 校验用户奖品限制
        Map<Long, JSONObject> awardUserTimesJsonMap = new HashMap<>();
        for (ActivityAwardAndSettingBo activityAwardAndSettingBo : activityAwardList) {
            JSONObject awardUserTimesJson = awardSettingService.AwardSettingRule(fansEntity.getFansId(), activityEntity.getAgentNo(), activityAwardAndSettingBo);
            if (ObjectUtils.isNotEmpty(awardUserTimesJson)) {
                awardUserTimesJsonMap.put(activityAwardAndSettingBo.getAwardId(), awardUserTimesJson);
            }
        }

        // 检测用户参与限制条件
        JSONObject limitJson = activitySettingService.checkActivitySetting(fansEntity, fansActivityDTO.getAppId(), redisOutTime, activityEntity);

        log.info("获取活动奖品返回的信息:{}", JSONUtil.toJsonStr(activityAwardList));
        for (ActivityAwardAndSettingBo activityAwardAndSettingBo : activityAwardList) {
            sendAwardService.sendCouponOrGoods(fansActivityDTO.getFansId(),fansActivityDTO.getAppId(),activityAwardList.get(0), null,null,true,
                    fansActivityDTO.getExtJson());

            //更新Redis中卡券每人参与的次数
            JSONObject awardUserTimesJson = awardUserTimesJsonMap.get(activityAwardAndSettingBo.getAwardId());
            awardSettingService.updateAwardLimitCache(awardUserTimesJson, redisOutTime);
        }

        //重新放置活动奖品配置缓存
        if (!activityAwardList.isEmpty()) {
            String activityAwardKey = String.format("%s%s:%s", Constants.REDIS_ACTIVITY.FILENAME, fansActivityDTO.getActivityId(), Constants.REDIS_ACTIVITY.AWARD);
            RedisUtil.set(activityAwardKey, activityAwardList, redisOutTime, TimeUnit.MILLISECONDS);
            log.info("活动奖品列表，修改奖品中的当前周期性领取次数数据，redisKey:{},redisValue:{},redisOutTime:{},TimeUnit:{}", activityAwardKey, JSONUtil.toJsonStr(activityAwardList), redisOutTime, "TimeUnit.MILLISECONDS");
        }

        //更新Redis中参与的次数
        ActivityUserTimesBO activityUserTimesBO = limitJson.getObject("activityUserTimesBO", ActivityUserTimesBO.class);
        if (ObjectUtils.isNotEmpty(activityUserTimesBO)){
            activitySettingService.updateRedisParticipateNum(limitJson.getString("currentTimesKey"), activityUserTimesBO, redisOutTime);
        }
        Boolean userJoinScoreValue = limitJson.getObject("userJoinScoreValue", Boolean.class);
        if (ObjectUtils.isNotEmpty(userJoinScoreValue)){
            RedisUtil.set(limitJson.getString("userJoinScoreKey"), userJoinScoreValue, redisOutTime, TimeUnit.MILLISECONDS);
        }

        return activityAwardService.toUserData(null);
    }

}
